<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>Insert title here</title>
	<link rel="stylesheet" type="text/css" href="css/login.css">
</head>
<body>

	<div id="banner" style="background-color: rgba(0,0,0,0.5);"></div>
	
	<div id="login">
		  <h1>Log in</h1>
		  <form>
			    <input type="username" placeholder="Username" name="username" />
			    <input type="password" placeholder="Password" name="password" />
			    <input type="submit" value="Log in" name ="submit" />
		  </form>
	</div>

	<%
		// Test commit
	
		String x = request.getParameter("submit");
		System.out.println("X : "+x);//try
	    if("Log in".equals(x)) {
		   	String username = request.getParameter("username");
		   	String password = request.getParameter("password");
		   
		   	System.out.println("username : "+username);//try
		   	System.out.println("password : "+password);//try
		   
			Connection connect = null;		
			try {
				Class.forName("com.mysql.jdbc.Driver");
			
				connect =  DriverManager.getConnection("jdbc:mysql://localhost/AIMS" +
						"?user=root&password=");
			
				if(connect != null){
	%>
					<script language="javascript"> alert("Database Connected");	</script>
		
	<%
				} else {
					out.println("Database Connect Failed.");	
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				out.println(e.getMessage());
				e.printStackTrace();
			}
		
		
		   try {			   
				ResultSet rs = connect.createStatement().executeQuery("select username, password " 
						+ " from user ");
				
				while(rs.next()) {
					String usernameDB = rs.getString("username");
					String passwordDB = rs.getString("password");
					
					if(username.equals(usernameDB) && password.equals(passwordDB)) {
						System.out.println("Correct!!");//try	
						
						// create session
						session.setAttribute("sUser", username);
						session.setAttribute("sPassword", password);
	%>
						<meta HTTP-EQUIV="Refresh" CONTENT="3; URL=hello.jsp">
	<%			
					}
					else {
						System.out.println("Noooooooooo!!");//try
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
	    } 
	%>
</body>
</html>